﻿using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RecruitStudents.Client.Examinee
{
    public partial class Examinee_ExamGroup : Utility.BaseForm
    {
        BLL.ServiceExaminee examineeService = new BLL.ServiceExaminee();
        public Examinee_ExamGroup()
        {
            InitializeComponent();
            BindBiaoYan();
        }

        private void BindBiaoYan()
        {
            gridControl1.DataSource = examineeService.GetTable("select * from TB_Examinee_ExamGroup where SpecialityKindsNO like '表演%'");
        }

        private void BindBoYin()
        {
            gridControl2.DataSource = examineeService.GetTable("select * from TB_Examinee_ExamGroup where SpecialityKindsNO like '播音%'");
        }

        private void btnDelete_BY_Click(object sender, EventArgs e)
        {
            examineeService.ExecuteNonQuery("delete TB_Examinee_ExamGroup where SpecialityKindsNO like '表演%'");
            BindBiaoYan();
        }

        string strDateTime_SW830 = "08:30";
        string strDateTime_SW930 = "09:30";

        string strDateTime_SW1030 = "10:30";
        string strDateTime_XW1300 = "13:00";
        string strDateTime_XW1400 = "14:00";

        string strDateTime_XW1230 = "12:30";
        string strDateTime_XW1330 = "13:30";
        string strDateTime_XW1430 = "14:30";
        string strDateTime_XW1530 = "15:30";
        string strDateTime_XW1630 = "16:30";

        private void btnView_Click(object sender, EventArgs e)
        {
            if (txtOneDay.Text == "" || txtTwoDay.Text == "")
            {
                DevHelper.ShowErrorMessageBox("请先选择考试时间");
                return;
            }
            OneDay_BiaoYan();
            TwoDay_BiaoYan();
            DevHelper.ShowInfoMessageBox("分配表演成功！");
            BindBiaoYan();
        }

        /// <summary>
        /// 第二天考试
        /// </summary>
        private void TwoDay_BiaoYan()
        {
            string strSpecName1 = "表演(声台形)";
            string strSpecName2 = "表演(表)";
            //取不包含已经分配了表演考生数据
            string strSql = @"select distinct b.AllowExamID,a.Name,a.IdentityCard,a.SignUpID from TB_SignUpStudents as a 
                        inner join TB_Exam_SignUpStudentsSpeciality as b 
                        on a.SignUpID = b.SignUpID and ISNULL(b.AllowExamID, '')<> ''
                        where b.SpecialityKindsNO like '1303010%'
                        and b.AllowExamID not in (
                            select AllowExamID from TB_Examinee_ExamGroup
	                   )
                ";
            TB_Examinee_ExamGroup model = null;
            string strAllowExamID = string.Empty;
            DataTable dt = examineeService.GetTable(strSql);
            //保存上午分配的
            List<TB_Examinee_ExamGroup> list_SW = new List<TB_Examinee_ExamGroup>();
            //保存多出的人
            List<TB_Examinee_ExamGroup> list_50 = new List<TB_Examinee_ExamGroup>();
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    model = new TB_Examinee_ExamGroup();
                    strAllowExamID = string.Empty;

                    strSql = "select AllowExamID from TB_Exam_SignUpStudentsSpeciality where SignUpID='" + dt.Rows[i]["SignUpID"].ToSafeString() + "' and SpecialityKindsNO like '1303010%'";
                    DataTable dtExam = examineeService.GetTable(strSql);
                    if (dtExam != null && dtExam.Rows.Count > 0)
                    {
                        strAllowExamID = dtExam.Rows[0]["AllowExamID"].ToSafeString();
                    }
                    if (i < 45)
                    {
                        model.SpecialityKindsNO = strSpecName1;
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_SW830;
                        model.AllowExamID = strAllowExamID;
                        list_SW.Add(model);
                    }
                    else if (i >= 45 && i < 90)
                    {
                        model.SpecialityKindsNO = strSpecName1;
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_SW930;
                        model.AllowExamID = strAllowExamID;
                        list_SW.Add(model);
                    }
                    else if (i >= 90 && i < 118)
                    {
                        model.SpecialityKindsNO = strSpecName2;
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_SW830;
                        model.AllowExamID = strAllowExamID;
                        list_SW.Add(model);
                    }
                    else if (i >= 118 && i < 146)
                    {
                        model.SpecialityKindsNO = strSpecName2;
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_SW930;
                        model.AllowExamID = strAllowExamID;
                        list_SW.Add(model);
                    }
                    else if (i >= 146 && i < 174)
                    {
                        model.SpecialityKindsNO = strSpecName2;
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_SW1030;
                        model.AllowExamID = strAllowExamID;
                        list_SW.Add(model);
                    }
                    else
                    {
                        //把多出的人放到下午
                        model.SpecialityKindsNO = strSpecName1;
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_XW1300;
                        model.AllowExamID = strAllowExamID;
                        list_50.Add(model);
                    }
                    examineeService.SaveExamGroup(model);
                }
            }
            int res = list_50.Count;
            foreach (TB_Examinee_ExamGroup mdl in list_SW)//循环取上午分配的人
            {
                if (mdl.SpecialityKindsNO == strSpecName2)//判断上午分配的人专业是否(表演(表))，把表演(表)的考生分配到表演(声台形)
                {
                    if (res < 67)
                    {
                        model = new TB_Examinee_ExamGroup();
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_XW1300;
                        model.AllowExamID = mdl.AllowExamID;
                        model.SpecialityKindsNO = strSpecName1;
                        examineeService.SaveExamGroup(model);
                        res = res + 1;
                    }
                    else if (res >= 67)
                    {
                        model = new TB_Examinee_ExamGroup();
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_XW1400;
                        model.AllowExamID = mdl.AllowExamID;
                        model.SpecialityKindsNO = strSpecName1;
                        examineeService.SaveExamGroup(model);
                        res = res + 1;
                    }
                }
            }
            //取上午表演(声台形)8:30、9：30的考生数据来分配下午的表演(表)
            strSql = "select * from TB_Examinee_ExamGroup where (grouptime='" + txtTwoDay.Text + " 08:30' or grouptime='" + txtTwoDay.Text + " 09:30') and SpecialityKindsNO='表演(声台形)' order by CreateDate";
            dt = examineeService.GetTable(strSql);
            if (dt != null)
            {
                for (int res_xw = 0; res_xw < dt.Rows.Count; res_xw++)
                {
                    DataRow dr = dt.Rows[res_xw];
                    model = new TB_Examinee_ExamGroup();
                    model.AllowExamID = dr["AllowExamID"].ToSafeString();
                    model.SpecialityKindsNO = strSpecName2;
                    if (res_xw < 28)
                    {
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_XW1230;
                    }
                    else if (res_xw >= 28 && res_xw < 56)
                    {
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_XW1330;
                    }
                    else if (res_xw >= 56 && res_xw < 84)
                    {
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_XW1430;
                    }
                    else if (res_xw >= 84 && res_xw < 112)
                    {
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_XW1530;
                    }
                    else if (res_xw >= 112 && res_xw < 140)
                    {
                        model.GroupTime = txtTwoDay.Text + " " + strDateTime_XW1630;
                    }
                    examineeService.SaveExamGroup(model);
                }
            }
            res = list_50.Count;//把多出的人放到表演(表)最后面
            foreach (TB_Examinee_ExamGroup md in list_50)
            {
                model = new TB_Examinee_ExamGroup();
                model.AllowExamID = md.AllowExamID;
                model.GroupTime = txtTwoDay.Text + " " + strDateTime_XW1630;
                model.SpecialityKindsNO = strSpecName2;
                examineeService.SaveExamGroup(model);
            }
        }

        /// <summary>
        /// 第一天考试
        /// </summary>
        private void OneDay_BiaoYan()
        {
            string strSpecName1 = "表演(声台形)";
            string strSpecName2 = "表演(表)";
            //取报了两个专业的优先分配表演
            string strSql = @"select a.Name,a.IdentityCard,a.SignUpID,b.sl from TB_SignUpStudents as a inner join (
                    select COUNT(distinct AllowExamID) as sl,SignUpID from TB_Exam_SignUpStudentsSpeciality 
                    where ISNULL(AllowExamID, '') <> '' group by SignUpID ) as b on a.SignUpID=b.SignUpID
                where sl=2 ";
            DataTable dt = examineeService.GetTable(strSql);
            TB_Examinee_ExamGroup model = null;
            string strAllowExamID = string.Empty;
            List<TB_Examinee_ExamGroup> list_SW = new List<TB_Examinee_ExamGroup>();//上午分配人数
            List<TB_Examinee_ExamGroup> list_50 = new List<TB_Examinee_ExamGroup>();//另外优先分配50人
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    model = new TB_Examinee_ExamGroup();
                    strAllowExamID = string.Empty;
                    strSql = "select AllowExamID from TB_Exam_SignUpStudentsSpeciality where SignUpID='" + dt.Rows[i]["SignUpID"].ToSafeString() + "' and SpecialityKindsNO like '1303010%'";
                    DataTable dtExam = examineeService.GetTable(strSql);
                    if (dtExam != null && dtExam.Rows.Count > 0)
                    {
                        strAllowExamID = dtExam.Rows[0]["AllowExamID"].ToSafeString();
                    }
                    if (i < 45)//表演(声台形)  8点30 分配45人
                    {
                        model.SpecialityKindsNO = strSpecName1;
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_SW830;
                        model.AllowExamID = strAllowExamID;
                        list_SW.Add(model);
                    }
                    else if (i >= 45 && i < 90)//表演(声台形)  9点30 分配45人
                    {
                        model.SpecialityKindsNO = strSpecName1;
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_SW930;
                        model.AllowExamID = strAllowExamID;
                        list_SW.Add(model);
                    }
                    else if (i >= 90 && i < 118)//表演(表)  8点30  分配28分  
                    {
                        model.SpecialityKindsNO = strSpecName2;
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_SW830;
                        model.AllowExamID = strAllowExamID;
                        list_SW.Add(model);
                    }
                    else if (i >= 118 && i < 146)//表演(表)  9点30  分配28分
                    {
                        model.SpecialityKindsNO = strSpecName2;
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_SW930;
                        model.AllowExamID = strAllowExamID;
                        list_SW.Add(model);
                    }
                    else if (i >= 146 && i < 174)//表演(表)  10点30  分配28分
                    {
                        model.SpecialityKindsNO = strSpecName2;
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_SW1030;
                        model.AllowExamID = strAllowExamID;
                        list_SW.Add(model);
                    }
                    else//表演(声台形)  13点00  优先分配余下的人
                    {
                        model.SpecialityKindsNO = strSpecName1;
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1300;
                        model.AllowExamID = strAllowExamID;
                        list_50.Add(model);
                    }
                    examineeService.SaveExamGroup(model);
                }
            }
            if (list_50.Count < 50)//当优先分配小于50人时候，取不包含已经分配的考生
            {
                int top = 50 - list_50.Count;
                strSql = @"select distinct top {0} b.AllowExamID,a.Name,a.IdentityCard,a.SignUpID from TB_SignUpStudents as a 
                        inner join TB_Exam_SignUpStudentsSpeciality as b 
                        on a.SignUpID = b.SignUpID and ISNULL(b.AllowExamID, '')<> ''
                        where b.SpecialityKindsNO like '1303010%'
                        and b.AllowExamID not in (
                            select AllowExamID from TB_Examinee_ExamGroup
	                   )
                ";
                strSql = string.Format(strSql, top);
                dt = examineeService.GetTable(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    model = new TB_Examinee_ExamGroup();
                    strAllowExamID = string.Empty;
                    strSql = "select AllowExamID from TB_Exam_SignUpStudentsSpeciality where SignUpID='" + dr["SignUpID"].ToSafeString() + "' and SpecialityKindsNO like '1303010%'";
                    DataTable dtExam = examineeService.GetTable(strSql);
                    if (dtExam != null && dtExam.Rows.Count > 0)
                    {
                        strAllowExamID = dtExam.Rows[0]["AllowExamID"].ToSafeString();
                    }
                    model.AllowExamID = strAllowExamID;
                    model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1300;
                    model.SpecialityKindsNO = strSpecName1;
                    if (strAllowExamID != string.Empty)
                    {
                        examineeService.SaveExamGroup(model);
                        list_50.Add(model);
                    }
                }
            }

            //分配表演(声台形)下午 时间
            int res = list_50.Count;
            foreach (TB_Examinee_ExamGroup mdl in list_SW)//循环取上午分配的人
            {
                if (mdl.SpecialityKindsNO == strSpecName2)//判断上午分配的人专业是否(表演(表))，把表演(表)的考生分配到表演(声台形)
                {
                    if (res < 67)
                    {
                        model = new TB_Examinee_ExamGroup();
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1300;
                        model.AllowExamID = mdl.AllowExamID;
                        model.SpecialityKindsNO = strSpecName1;
                        examineeService.SaveExamGroup(model);
                        res = res + 1;
                    }
                    else if (res >= 67)
                    {
                        model = new TB_Examinee_ExamGroup();
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1400;
                        model.AllowExamID = mdl.AllowExamID;
                        model.SpecialityKindsNO = strSpecName1;
                        examineeService.SaveExamGroup(model);
                        res = res + 1;
                    }
                }
            }
            //取上午表演(声台形)8:30、9：30的考生数据来分配下午的表演(表)
            strSql = "select * from TB_Examinee_ExamGroup where (grouptime='" + txtOneDay.Text + " 08:30' or grouptime='" + txtOneDay.Text + " 09:30') and SpecialityKindsNO='表演(声台形)' order by CreateDate";
            dt = examineeService.GetTable(strSql);
            if (dt != null)
            {
                for (int res_xw = 0; res_xw < dt.Rows.Count; res_xw++)
                {
                    DataRow dr = dt.Rows[res_xw];
                    model = new TB_Examinee_ExamGroup();
                    model.AllowExamID = dr["AllowExamID"].ToSafeString();
                    model.SpecialityKindsNO = strSpecName2;//表演(表)
                    if (res_xw < 28)
                    {
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1230;
                    }
                    else if (res_xw >= 28 && res_xw < 56)
                    {
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1330;
                    }
                    else if (res_xw >= 56 && res_xw < 84)
                    {
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1430;
                    }
                    else if (res_xw >= 84 && res_xw < 112)
                    {
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1530;
                    }
                    else if (res_xw >= 112 && res_xw < 140)
                    {
                        model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1630;
                    }
                    examineeService.SaveExamGroup(model);
                }
            }
            //把表演(声台形)优先50人的分配到最后(表演(表))
            res = list_50.Count - 28;
            foreach (TB_Examinee_ExamGroup md in list_50)
            {
                model = new TB_Examinee_ExamGroup();
                if (res >= 22 && res <= 50)
                {
                    model.AllowExamID = md.AllowExamID;
                    model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1530;
                    model.SpecialityKindsNO = strSpecName2;
                    res = res + 1;
                }
                else
                {
                    model.AllowExamID = md.AllowExamID;
                    model.GroupTime = txtOneDay.Text + " " + strDateTime_XW1630;
                    model.SpecialityKindsNO = strSpecName2;
                    res = res + 1;
                }
                examineeService.SaveExamGroup(model);
            }
        }

        private void btnBoYin_Click(object sender, EventArgs e)
        {
            if (txtOneDay.Text == "" || txtTwoDay.Text == "")
            {
                DevHelper.ShowErrorMessageBox("请先选择考试时间");
                return;
            }
            string strSpecName1 = "播音";
            string strSpecName2 = "";
         
            DevHelper.ShowInfoMessageBox("分配播音成功！");
            BindBiaoYan();
        }

        private void btnDeleteBoYin_Click(object sender, EventArgs e)
        {
            examineeService.ExecuteNonQuery("delete TB_Examinee_ExamGroup where SpecialityKindsNO like '播音%'");
            BindBiaoYan();
        }
    }
}
